package com.xbzl.cly_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbzl.cly_demo.dto.VehicleTrackDTO;
import com.xbzl.cly_demo.entity.VehicleStatus;
import com.xbzl.cly_demo.exception.VehicleStatusNotFoundException;
import com.xbzl.cly_demo.service.VehicleStatusService;
import com.xbzl.cly_demo.mapper.VehicleStatusMapper;
import com.xbzl.cly_demo.vo.VehicleStatusVO;
import com.xbzl.cly_demo.vo.VehicleTrackVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 26586
* @description 针对表【vehicle_status(车辆状态表，存储随时间变化的数据)】的数据库操作Service实现
* @createDate 2025-04-03 16:27:17
*/
@Service
public class VehicleStatusServiceImpl extends ServiceImpl<VehicleStatusMapper, VehicleStatus>
    implements VehicleStatusService{

    @Autowired
    VehicleStatusMapper vehicleStatusMapper;
    @Override
    public VehicleStatusVO queryVehicleStatusByVin(String vehicleVin) {
        // 1. 查询车辆状态表，获取前一秒的车辆状态
        long time = new Date().getTime();
        VehicleStatus vehicleStatus = vehicleStatusMapper.selectList(new QueryWrapper<VehicleStatus>()
                .eq("vehicle_vin", vehicleVin)
                .between("timestamp_gnss", time - 1000, time))
                .get(0);
        // 2. 如果没有找到车辆状态，返回 null
        if (vehicleStatus == null) {
            throw new VehicleStatusNotFoundException("车辆状态未找到");
        }
        // 3. 将查询到的车辆状态转换为 VO 对象
        VehicleStatusVO vehicleStatusVO = new VehicleStatusVO();
        BeanUtils.copyProperties(vehicleStatus, vehicleStatusVO);
        // 4. 返回 VO 对象
        return vehicleStatusVO;
    }

    @Override
    public VehicleTrackVO queryTrack(VehicleTrackDTO vehicleTrackDTO) {
        return null;

    }
}
